/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vista;


import dao.ArticuloDao;
import dao.imp.ArticuloDaoImp;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import modelo.dominio.Articulo;
import modelo.dominio.Imprimir;
import modelo.dominio.Usuario;


/**
 *
 * @author Pablo
 */
public class UIMenu extends javax.swing.JFrame {
        ArticuloDao dao = new ArticuloDaoImp();
        ArrayList<Articulo> lista = new ArrayList<Articulo>();
         ArrayList<Articulo> vendidos=new ArrayList<Articulo>();
        Articulo at = new Articulo();  
       
        
        Calendar c1 = Calendar.getInstance();
       
        String dia=Integer.toString(c1.get(Calendar.DATE));
        String mes=Integer.toString(c1.get(Calendar.MONTH)+1);
        String annio=Integer.toString(c1.get(Calendar.YEAR));
        
       
            
               
    /**
     * Creates new form UIMenu
     */
        
        public UIMenu() {
        initComponents();
        }
        
        
    public UIMenu(String Administrador) {
        initComponents();
          lista=dao.getAll();
          this.cargarTabla();
//          this.txtFecha.setHorizontalAlignment(JTextField.CENTER);
//          this.txtFecha.setText(dia+"-"+mes+"-"+annio);
          this.lblUsuario.setText("Administrador: "+Administrador);  
        
      
                
    }
     public UIMenu(Usuario usuario) {
         
        initComponents();
           Usuario u = usuario;
          lista=dao.getAll();
          this.cargarTabla();
          this.mnuAdmin.setEnabled(false);
//          this.txtFecha.setHorizontalAlignment(JTextField.CENTER);
//          this.txtFecha.setText(dia+"-"+mes+"-"+annio);
          this.lblUsuario.setText("Usuario: "+usuario.getNombre()+" "+usuario.getApellido());
     
      
    }
  
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane3 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        btnVender = new javax.swing.JButton();
        btnEliminar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        txtSubTotal = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblVenta = new javax.swing.JTable();
        txtTotal1 = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtCodigo = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        txtCantidad = new javax.swing.JTextField();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblBuscar = new javax.swing.JTable();
        txtArticulo = new javax.swing.JTextField();
        btnActualizar = new javax.swing.JButton();
        lblUsuario = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        mnuCambio = new javax.swing.JMenuItem();
        mnuAdmin = new javax.swing.JMenu();
        jMenu3 = new javax.swing.JMenu();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenuItem3 = new javax.swing.JMenuItem();
        jMenuItem5 = new javax.swing.JMenuItem();
        jMenuItem6 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("La Tranquera");

        jPanel1.setBackground(new java.awt.Color(204, 204, 255));

        btnVender.setText("Vender");
        btnVender.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnVenderActionPerformed(evt);
            }
        });

        btnEliminar.setText("Eliminar");
        btnEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEliminarActionPerformed(evt);
            }
        });

        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel2.setText("Sub Total:");

        txtSubTotal.setFont(new java.awt.Font("Verdana", 1, 36)); // NOI18N
        txtSubTotal.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtSubTotalActionPerformed(evt);
            }
        });

        tblVenta.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Nombre", "Cantidad", "Precio"
            }
        ));
        tblVenta.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                Eliminar(evt);
            }
        });
        jScrollPane1.setViewportView(tblVenta);

        txtTotal1.setFont(new java.awt.Font("Verdana", 1, 36)); // NOI18N
        txtTotal1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtTotal1ActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel3.setText("TOTAL:");

        txtCodigo.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        txtCodigo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtCodigoActionPerformed(evt);
            }
        });
        txtCodigo.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtCodigovenderPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtCodigoVender(evt);
            }
        });

        jLabel1.setText("Código");

        jLabel4.setText("Cantidad");

        txtCantidad.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        txtCantidad.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtCantidadActionPerformed(evt);
            }
        });
        txtCantidad.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtCantidadvenderPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtCantidadVender(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(txtSubTotal, javax.swing.GroupLayout.PREFERRED_SIZE, 291, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel1)
                                    .addComponent(txtCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 196, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel4)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addComponent(txtCantidad, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(btnVender, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(31, 31, 31))))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 291, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                        .addGap(29, 29, 29)
                                        .addComponent(btnEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(83, 83, 83)
                                        .addComponent(btnCancelar)))
                                .addGap(84, 84, 84)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(txtTotal1, javax.swing.GroupLayout.PREFERRED_SIZE, 313, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addGap(10, 10, 10)
                                        .addComponent(jLabel3)))))
                        .addContainerGap(166, Short.MAX_VALUE))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtCantidad, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(5, 5, 5))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(btnVender, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(7, 7, 7)))
                .addGap(28, 28, 28)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addGap(18, 18, 18)
                        .addComponent(txtTotal1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 277, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jLabel2)
                .addGap(14, 14, 14)
                .addComponent(txtSubTotal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(25, Short.MAX_VALUE))
        );

        jTabbedPane3.addTab("Venta", jPanel1);

        jPanel4.setBackground(new java.awt.Color(204, 204, 255));

        tblBuscar.setBackground(new java.awt.Color(255, 255, 204));
        tblBuscar.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true));
        tblBuscar.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Codigo", "Nombre", "Precio ", "Stock"
            }
        ));
        jScrollPane2.setViewportView(tblBuscar);

        txtArticulo.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtArticuloKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtArticuloKeyReleased(evt);
            }
        });

        btnActualizar.setText("Actualizar");
        btnActualizar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnActualizarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                .addContainerGap(43, Short.MAX_VALUE)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(txtArticulo, javax.swing.GroupLayout.PREFERRED_SIZE, 280, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnActualizar, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 780, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(41, 41, 41))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtArticulo, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnActualizar, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 454, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(41, Short.MAX_VALUE))
        );

        jTabbedPane3.addTab("Búsqueda", jPanel4);

        lblUsuario.setFont(new java.awt.Font("MS Reference Sans Serif", 1, 14)); // NOI18N

        jMenu1.setText("Archivo");

        jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F4, java.awt.event.InputEvent.ALT_MASK));
        jMenuItem1.setText("Salir");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        mnuCambio.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F5, java.awt.event.InputEvent.ALT_MASK));
        mnuCambio.setText("Cambio de Usuario");
        mnuCambio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuCambioActionPerformed(evt);
            }
        });
        jMenu1.add(mnuCambio);

        jMenuBar1.add(jMenu1);

        mnuAdmin.setText("Veterinaria");

        jMenu3.setText("Gestión");

        jMenuItem2.setText("Lista de Articulos");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem2);

        jMenuItem3.setText("Lista de Categorias");
        jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem3ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem3);

        jMenuItem5.setText("Lista Proveedores");
        jMenuItem5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem5ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem5);

        jMenuItem6.setText("Usuarios");
        jMenuItem6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem6ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem6);

        mnuAdmin.add(jMenu3);

        jMenuBar1.add(mnuAdmin);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(98, 98, 98)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, 280, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTabbedPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 869, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(0, 100, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 588, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(lblUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(94, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
       
        System.exit(0);
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
      
        UIArticuloABML ventana = new UIArticuloABML(this, true);
        ventana.setLocationRelativeTo(this);
        ventana.setVisible(true);
    }//GEN-LAST:event_jMenuItem2ActionPerformed

    private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed
        
        UICategoriaABML ventanaC = new UICategoriaABML(this, true);
        ventanaC.setLocationRelativeTo(this);
        ventanaC.setVisible(true);
    }//GEN-LAST:event_jMenuItem3ActionPerformed

    private void jMenuItem5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem5ActionPerformed
        // TODO add your handling code here:
        UIProveedorABML ventanaP = new UIProveedorABML(this, true);
        ventanaP.setLocationRelativeTo(this);
        ventanaP.setVisible(true);
    }//GEN-LAST:event_jMenuItem5ActionPerformed

    private void jMenuItem6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem6ActionPerformed
        // TODO add your handling code here:
        UIUsuariosABML ventanaU = new UIUsuariosABML(this, true);
        ventanaU.setLocationRelativeTo(this);
        ventanaU.setVisible(true);
    }//GEN-LAST:event_jMenuItem6ActionPerformed

    private void txtArticuloKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtArticuloKeyReleased
                     
        if (evt.getKeyCode() == evt.VK_BACK_SPACE){           
                 txtArticulo.setText(null);
                 this.limpiarTabla();
                 this.cargarTabla();   
        }           
    }//GEN-LAST:event_txtArticuloKeyReleased

    private void txtArticuloKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtArticuloKeyPressed
        // TODO add your handling code here    
         String art=txtArticulo.getText();
             Pattern  p =Pattern.compile(art);
             ArrayList<Articulo> encontrados=new ArrayList<Articulo>();
             for (int i = 0; i < lista.size(); i++) {
             String campo= lista.get(i).getNombre();               
               Matcher  m = p.matcher(campo);
                  if (m.find()){                    
                      int id= lista.get(i).getId();
                      at=dao.getArticulo(id);                
                        encontrados.add(at);                                          
                  }                
             }
             this.limpiarTabla();
             this.cargarTabla(encontrados);   
    }//GEN-LAST:event_txtArticuloKeyPressed
 
    private void btnActualizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnActualizarActionPerformed
        // TODO add your handling code here:
            this.limpiarTabla();
            this.lista=dao.getAll();
            this.cargarTabla();
    }//GEN-LAST:event_btnActualizarActionPerformed

    private void txtCantidadVender(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtCantidadVender
        // TODO add your handling code here:
        if (evt.getKeyCode() == evt.VK_ENTER) {

            String cod = txtCodigo.getText();
            double total = 0;
            Articulo articulo = dao.getArticuloByCodigo(cod);


            if (articulo != null) {
                DefaultTableModel modelo5 = (DefaultTableModel) tblVenta.getModel();
                String datos[] = new String[3];
                datos[0] = articulo.getNombre();
                int cant = Integer.parseInt(this.txtCantidad.getText());
                datos[1] = "X " + cant;

                double pr = articulo.getPrecioVenta();
                double tl = pr * cant;
                datos[2] = String.valueOf(redondearTablaBusqueda(tl));
                modelo5.addRow(datos);
                vendidos.add(articulo);

            } else {
                JOptionPane.showMessageDialog(this, "El Producto No Existe");
            }
            for (int j = 0; j < tblVenta.getRowCount(); j++) {
                String p = (String) tblVenta.getValueAt(j, 2);
                double pv = Double.parseDouble(p);
                total = total + pv;
                this.redondearVendidos(total);
            }
        }

    }//GEN-LAST:event_txtCantidadVender

    private void txtCantidadvenderPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtCantidadvenderPressed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtCantidadvenderPressed

    private void txtCantidadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtCantidadActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtCantidadActionPerformed

    /*
     * jtextfield Vender
     */
    private void txtCodigoVender(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtCodigoVender
        // TODO add your handling code here:
        if (evt.getKeyCode() == evt.VK_ENTER) {
            this.txtCodigo.selectAll();
            String cod = txtCodigo.getText();
            double total = 0;
            Articulo articulo = dao.getArticuloByCodigo(cod);

            if (articulo != null) {
                DefaultTableModel modelo5 = (DefaultTableModel) tblVenta.getModel();
                String datos[] = new String[3];
                datos[0] = articulo.getNombre();
                
               
                int cant2 = 1;
               
                datos[1] = "X " + cant2;

                double pr = articulo.getPrecioVenta();
                double tl = pr * cant2;
                datos[2] = String.valueOf(redondearTablaBusqueda(tl));
                modelo5.addRow(datos);
                vendidos.add(articulo);

            } else {
                JOptionPane.showMessageDialog(this, "El Producto No Existe");
            }

            for (int j = 0; j < tblVenta.getRowCount(); j++) {
                String p = (String) tblVenta.getValueAt(j, 2);
                double pv = Double.parseDouble(p);
                total = total + pv;
                this.redondearVendidos(total);
            }
        }
    }//GEN-LAST:event_txtCodigoVender

    private void txtCodigovenderPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtCodigovenderPressed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtCodigovenderPressed

    private void txtCodigoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtCodigoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtCodigoActionPerformed

    private void Eliminar(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_Eliminar

        if (evt.getKeyCode() == evt.VK_DELETE) {

            this.eliminarElemento();

        }
    }//GEN-LAST:event_Eliminar

    private void txtSubTotalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtSubTotalActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtSubTotalActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        // TODO add your handling code here:
        this.txtCodigo.setText(null);
        this.txtSubTotal.setText(null);
        this.txtCantidad.setText(null);
        vendidos.removeAll(vendidos);
        this.limpiarVenta();
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void btnEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEliminarActionPerformed
        // TODO add your handling code here:
        this.eliminarElemento();

    }//GEN-LAST:event_btnEliminarActionPerformed

    private void btnVenderActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnVenderActionPerformed
        // TODO add your handling code here:
        for (int i = 0; i < vendidos.size(); i++) {
            for (int j = 0; j < lista.size(); j++) {
                if (vendidos.get(i).getNombre().equals(lista.get(j).getNombre())) {
                    Articulo des = new Articulo();
                    des = lista.get(j);
                    int d = des.getStock();
                    d = d - 1;
                    des.setStock(d);
                    dao.update(des);
                }
            }
        }
        JOptionPane.showMessageDialog(this, "Venta Realizada con Exito");
        this.txtCodigo.setText(null);
        this.txtSubTotal.setText(null);
        vendidos.removeAll(vendidos);
        this.limpiarVenta();

        String fecha;
        fecha = dia + "-" + mes + "-" + annio;
        String ticket;
        ticket = "Nro. de ticket";

        Imprimir f = new Imprimir();
        f.impresion(fecha);

    }//GEN-LAST:event_btnVenderActionPerformed

    private void txtTotal1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtTotal1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtTotal1ActionPerformed

    private void mnuCambioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuCambioActionPerformed
        // TODO add your handling code here:
        this.dispose();
         UILogin login = new UILogin(null, true);
       login.setLocationRelativeTo(null);
       login.setVisible(true); 
    }//GEN-LAST:event_mnuCambioActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(UIMenu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(UIMenu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(UIMenu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(UIMenu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new UIMenu().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnActualizar;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnEliminar;
    private javax.swing.JButton btnVender;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItem5;
    private javax.swing.JMenuItem jMenuItem6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTabbedPane jTabbedPane3;
    private javax.swing.JLabel lblUsuario;
    private javax.swing.JMenu mnuAdmin;
    private javax.swing.JMenuItem mnuCambio;
    private javax.swing.JTable tblBuscar;
    private javax.swing.JTable tblVenta;
    private javax.swing.JTextField txtArticulo;
    private javax.swing.JTextField txtCantidad;
    private javax.swing.JTextField txtCodigo;
    private javax.swing.JTextField txtSubTotal;
    private javax.swing.JTextField txtTotal1;
    // End of variables declaration//GEN-END:variables
    
       private void CargarVenta(ArrayList<Articulo> vendidos) {
         DefaultTableModel modelo5 = (DefaultTableModel) tblVenta.getModel();
         
            String datos[] = new String[3];
            for(Articulo a : vendidos){
            datos[0]=a.getNombre();            
            datos[1]=String.valueOf(a.getPrecioVenta());                
            modelo5.addRow(datos);   
            }
    }
    private void cargarTabla() {
        DefaultTableModel modelo = (DefaultTableModel) tblBuscar.getModel();
       
            String data[] = new String[4];
            
            for(Articulo a : lista){
                data[0]=a.getBarCode();
                data[1]=a.getNombre();
                data[2]=String.valueOf(a.getPrecioVenta());
                data[3]=String.valueOf(a.getStock());
                modelo.addRow(data);
            }
    }
    
    private void limpiarTabla() {
        DefaultTableModel modelo = (DefaultTableModel) tblBuscar.getModel();
                      while(this.tblBuscar.getRowCount() !=0)
                             modelo.removeRow(0);       
    }
    
     private void cargarTabla(ArrayList<Articulo> buscados) {
        DefaultTableModel modelo = (DefaultTableModel) tblBuscar.getModel();       
            String data[] = new String[4];           
            for(Articulo a : buscados){
                data[0]=a.getBarCode();
                data[1]=a.getNombre();              
                data[2]=String.valueOf(a.getPrecioVenta());
                data[3]=String.valueOf(a.getStock());
                modelo.addRow(data);
            }
    }
       private void limpiarVenta() {
        DefaultTableModel modelo = (DefaultTableModel) tblVenta.getModel();
                      while(this.tblVenta.getRowCount() !=0)
                             modelo.removeRow(0);                             
    }
    private void redondearVendidos(double nro) {       
      String val = nro+"";
      BigDecimal big = new BigDecimal(val);
      big = big.setScale(2, RoundingMode.HALF_UP);
      this.txtSubTotal.setText(big.toString());    
    }
     private double redondearTablaBusqueda(double nro) { 
      double fin=0;
      String val = nro+"";
      BigDecimal big = new BigDecimal(val);
      big = big.setScale(2, RoundingMode.HALF_UP);
      fin=big.doubleValue();
      return fin;
    }
    private void eliminarElemento() {   
        DefaultTableModel modelo5 = (DefaultTableModel) tblVenta.getModel();
            int fil=tblVenta.getSelectedRow();           
            double total=Double.parseDouble(this.txtSubTotal.getText());
            String e = (String) tblVenta.getValueAt(tblVenta.getSelectedRow(), 2); 
            double elim=Double.parseDouble(e);
            total=total-elim;
            this.redondearVendidos(total);
            modelo5.removeRow(fil);      
    }
    
    public String calcularVenta(){   
         String c;
        if(txtCantidad.getText() == null){
                 txtCantidad.setText("1");
                 c=txtCantidad.getText().trim();
            }
        else{
            c=txtCantidad.getText().trim();
        
    }
         return c;   
    
    }
}